/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.test;

import com.ejb.metier.IBookManager;
import com.ejb.metier.IOrderManager;
import com.ejb.metier.IUserManager;
import com.ejb.persistence.Address;
import com.ejb.persistence.Book;
import com.ejb.persistence.BookCategory;
import com.ejb.persistence.Order;
import com.ejb.persistence.User;
import com.ejb.persistence.UserAuthentication;
import com.ejb.persistence.UserType;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import javax.naming.NamingException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author Adel Boukelt
 */
public class ProcedureEJB_JUnit {
    
    private static Context ctx;
    private static EJBContainer container;
    public ProcedureEJB_JUnit() {
    }
    
    @BeforeClass
    public static void setUpClass() {
        container = EJBContainer.createEJBContainer();
        ctx = container.getContext();
    }
    
    @AfterClass
    public static void tearDownClass() {
        container.close();
    }
    
    @Before
    public void setUp() {
        
    }
    
    @After
    public void tearDown() {
        
    }
    
    @Test
    /*
     * Test package 1
     * purpose : verify the inscription/Authentification/Commande
     * 
     */
    public void Procedure1(){
        try {
            System.out.println("-------------------------------------------------");
            System.out.println("Test of procedure1");
            System.out.println("-------------------------------------------------");
            IUserManager userManager = (IUserManager) ctx.lookup("java:global/classes/UserManagerBean");
            User user = new User();
            user.setFirstName("Peter");
            user.setLastName("Parker");
            user.setEmail("jv1qsj@gmdsail.com");
            user.setType(UserType.Utilisateur);
            Address address = new Address();
            address.setCity("Aix");
            address.setCountry("France");
            address.setLine1("1600 rte des milles");
            address.setLine2("");
            address.setZipCode("13090");
            address.setUser(user);
            user.setAddress(address);
            UserAuthentication userAuth = new UserAuthentication();
            userAuth.setLogin("Procedure1");
            userAuth.setPassword("test");
            user.setUserAuth(userAuth);
            boolean resultInscription = userManager.createAccount(user, address);
            System.out.println("Le boolean retourné par la methode createAccount = "+resultInscription);

            User resultAuth = userManager.validateConn("Procedure1", "test");
            System.out.println("Le user retourné par la méthode validateConn s'appel = "+ resultAuth.getFirstName()+" "+resultAuth.getLastName());
            IOrderManager t = (IOrderManager) ctx.lookup("java:global/classes/OrderManagerBean");
            Long id = 15l;
            HashMap<Long, Integer> listBook = new HashMap<Long, Integer>();
            listBook.put(id, 4);
            listBook.put(id, 5);
            boolean i = t.addOrder(id, listBook);
            System.out.println("Le boolean retourné par la methode addOrder = "+i);
        } catch (NamingException ex) {
            Logger.getLogger(PersistenceJUnitTest.class.getName()).log(Level.SEVERE, null, ex);

        }
        System.out.println("-------------------------------------------------");
        System.out.println("--------------End of the Test--------------------");
        System.out.println("-------------------------------------------------");        
    }

    @Test
    /*
     * Test package 2
     * purpose : verify the Ajout/Modification/suppression for one book
     * 
     */
    public void Procedure2(){
        try {
            System.out.println("-------------------------------------------------");
            System.out.println("Test of procedure2");
            System.out.println("-------------------------------------------------");
            IBookManager bookManager = (IBookManager) ctx.lookup("java:global/classes/BookManagerBean");
            
            String title = "Le retour du test JUnit";
            float price = 15f;
            int stock = 25;
            int th = 5;
            String editor = "Framework";
            String sum = "Test de la procedure2";
            ArrayList<String> authors = new ArrayList<String>();
            authors.add("3");
            authors.add("10");
            
            boolean res = bookManager.insertBook(title, price, stock, th, "", editor, new Date(), sum, "", true, authors, BookCategory.Fantasy);
            System.out.println("Le boolean retourné par la methode insertBook = "+res);
            List<Book> list = new ArrayList<Book>();
            list = bookManager.getBooks("key", title, BookCategory.Fantasy);
            Book book1 = list.get(0);
            book1.setSummary("nouveau sommaire");
            book1.setPrice(50f);
            
            boolean res2 = bookManager.updateBook(book1, null);
            System.out.println("Le boolean retourné par la methode updateBook = "+res);
            //Le delete est en faite un update du champs available passé à false
            book1.setAvailable(false);
            boolean res3 = bookManager.updateBook(book1, null);
            System.out.println("Le boolean retourné par la methode updateBook = "+res);
            list.clear();
            list = bookManager.getBooks("key", title, BookCategory.Fantasy);
            System.out.println("Le livre est maintenant plus accessible par l'utilisateur excepté en vision sur ces historique de commande = "+res3);
            list.clear();
            list = bookManager.getMostSold();
            int z = 1;
            System.out.println("Voici la liste des livre :");
            System.out.println("------------------------------------------------------");
            for(Book b:list){

                    System.out.println("livre N°"+z+" - "+b.getTitle());
                    System.out.println("------------------------------------------------------");

                    z++;
            }
        } catch (NamingException ex) {
            Logger.getLogger(PersistenceJUnitTest.class.getName()).log(Level.SEVERE, null, ex);

        }
        System.out.println("-------------------------------------------------");
        System.out.println("--------------End of the Test--------------------");
        System.out.println("-------------------------------------------------");        
    }
    @Test
    /*
     * Test package 2
     * purpose : verify the UpdateProfile/Historique for one user
     * 
     */
    public void Procedure3(){
        try {
            System.out.println("-------------------------------------------------");
            System.out.println("Test of procedure3");
            System.out.println("-------------------------------------------------");
            IOrderManager orderManager = (IOrderManager) ctx.lookup("java:global/classes/OrderManagerBean");
            IUserManager userManager = (IUserManager) ctx.lookup("java:global/classes/UserManagerBean");
            //userManager
            
            List<Order> listOrder = new ArrayList<Order>();
            listOrder = orderManager.getOrders (15l, false);
            System.out.println("Liste des commandes effectuées pour "+listOrder.get(0).getUser().getFirstName()+" "+listOrder.get(0).getUser().getLastName());
            System.out.println("------------------------------------------------------");
            for(Order order:listOrder){

                System.out.println("livre N°"+order.getId()+" - pour l'ID N°"+order.getUser().getId());
                System.out.println("------------------------------------------------------");
            }
            User user = listOrder.get(0).getUser();
            user.setLastName("Nouveauv LastName");
            user.getAddress().setCountry("Exia");
            boolean res = userManager.updateAccount(user, user.getAddress());
            System.out.println("Le boolean retourné par la methode updateAccount = "+res);
               
        } catch (NamingException ex) {
            Logger.getLogger(PersistenceJUnitTest.class.getName()).log(Level.SEVERE, null, ex);

        }
        System.out.println("-------------------------------------------------");
        System.out.println("--------------End of the Test--------------------");
        System.out.println("-------------------------------------------------");        
    }
}
